package com.zh.baselibrary.data;


import org.xutils.DbManager;
import org.xutils.db.table.TableEntity;
import org.xutils.x;

public class DataBaseCreator {
    private static DbManager db;

    private static DataBaseCreator dataBaseCreator;

    private DataBaseCreator() {
    }

    public static DataBaseCreator getInstance() {
        if (dataBaseCreator == null) {
            dataBaseCreator = new DataBaseCreator();
        }
        return dataBaseCreator;
    }

    public DbManager getDbManager() {
//        if (db == null) {
        DbManager.DaoConfig daoConfig = getDaoConfig();
        db = x.getDb(daoConfig);
//        }
        return db;
    }


    /***
     * DaoConfig配置
     *
     * @return
     */
    private DbManager.DaoConfig getDaoConfig() {

        DbManager.DaoConfig daoConfig = new DbManager.DaoConfig()
                .setAllowTransaction(true)
                //设置表创建的监听
                .setTableCreateListener(new DbManager.TableCreateListener() {
                    @Override
                    public void onTableCreated(DbManager db, TableEntity<?> table) {

                    }
                })
                //设置数据库的版本号
                .setDbVersion(1)
                //设置数据库更新的监听
                .setDbUpgradeListener(new DbManager.DbUpgradeListener() {
                    @Override
                    public void onUpgrade(DbManager db, int oldVersion,
                                          int newVersion) {


                    }
                })
                //设置数据库打开的监听
                .setDbOpenListener(new DbManager.DbOpenListener() {
                    @Override
                    public void onDbOpened(DbManager db) {
                        //开启数据库支持多线程操作，提升性能
                        db.getDatabase().enableWriteAheadLogging();
                    }
                });

        daoConfig.setDbName("bookCorner");


        return daoConfig;
    }


}
